﻿using System;
using System.IO;
using System.Reflection;
using System.Web;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using AnyOffice.Components;
using log4net;

namespace AnyOffice.general.gzzd
{
    public partial class newfolder : ModulePage
    {
        private static ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                string str = (Request.QueryString["parent_folder"] != null) ? Request.QueryString["parent_folder"].ToString() : "/";
                this.ViewState["ParentFolder"] = str;
                GzzdInfo gzzdInfo = GzzdFactory.GetGzzdInfo();
                if (gzzdInfo == null)
                {
                    Response.Redirect("index.aspx");
                }
                else if (!gzzdInfo.CanNew && !gzzdInfo.CanManage)
                {
                    Response.Redirect("index.aspx");
                }
                else
                {
                    this.litNavTitle.Text = gzzdInfo.Name;
                    this.parentPath.DataSource = new LiteUtility().GetDtbDirectory();
                    this.parentPath.DataTextField = "path";
                    this.parentPath.DataValueField = "path";
                    this.parentPath.DataBind();
                    if (this.parentPath.Items.FindByValue(str) != null)
                    {
                        this.parentPath.Items.FindByValue(str).Selected = true;
                    }
                    this.btnSubmit.Attributes.Add("onclick", "return CheckForm();");
                }
            }
        }
        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("gzzd.aspx?path=" + Server.UrlEncode(this.ViewState["ParentFolder"].ToString()));
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            this.pnlMain.Visible = false;
            string str = "\\/:*?\"<>|";
            for (int i = 0; i < str.Length; i++)
            {
                if (this.txtFolderName.Text.IndexOf(str.Substring(i, 1)) >= 0)
                {
                    this.litMessage.Text = base.ShowPrompt("文件夹名含有不合法字符(如\\/:*?\"<>|等)，请重新输入！", "newfolder.aspx?parent_folder=" + Server.UrlEncode(this.ViewState["ParentFolder"].ToString()));
                    return;
                }
            }
            string path = (GzzdFactory.GetGzzdInfo().DiskPath + this.parentPath.Value).Replace("/", @"\");
            if (!path.EndsWith(@"\"))
            {
                path = path + @"\";
            }
            path = path + this.txtFolderName.Text;
            try
            {
                Directory.CreateDirectory(path);
            }
            catch (Exception exception)
            {
                if (log.IsErrorEnabled)
                {
                    log.Error("", exception);
                }
                this.litMessage.Text = base.ShowPrompt("新建文件夹失败！", "gzzd.aspx?path=" + Server.UrlEncode(this.ViewState["ParentFolder"].ToString()));
                return;
            }
            Response.Redirect("gzzd.aspx?path=" + Server.UrlEncode(this.ViewState["ParentFolder"].ToString()));
        }
    }
}
